package com.org.gundutubruk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

public class GameMode extends Activity{
	ImageView mode1;
	ImageView mode2;
	ImageView mode3;
	ImageView mode4;
	int mode;
	static boolean isDown=false;
	static boolean isSelect=false;
	static float currx=0;
	static float awal=0;
	static float nowx;
	
	private int mode1x;
	private int mode2x;
	private int mode3x;
	private int mode4x;
	private int width;
	Button help;
	
	private void nextAct3() {
		this.startActivity(new Intent("android.intent.action.MENUUTAMA"));
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
	}
	
	@Override
	public void onBackPressed(){
		nextAct3();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mode=2;
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.game_mode_layout);
		help=(Button)findViewById(R.id.help);
		help.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				helpact();
			}
		});
		width=getWindowManager().getDefaultDisplay().getWidth();
		mode1=(ImageView)findViewById(R.id.mode1);
		mode2=(ImageView)findViewById(R.id.mode2);
		mode3=(ImageView)findViewById(R.id.mode3);
		mode4=(ImageView)findViewById(R.id.mode4);
		mode1x=200-30;
		mode2x=-(width/2-200/2);
		mode3x=-(width-30);
		mode4x=mode3x-230;
		mode1.scrollTo(mode1x, 0);
		mode2.scrollTo(mode2x, 0);
		mode3.scrollTo(mode3x, 0);
		mode4.scrollTo(mode4x, 0);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event){
		final int actioncode = event.getAction() & MotionEvent.ACTION_MASK;
		
		if(event.getY()>=60&&event.getY()<=360){
			if(actioncode==MotionEvent.ACTION_DOWN){
				awal=event.getX();
				currx=event.getX();
				//if(mode!=1||mode!=3){
					isDown=true;
				if(currx>=60&&currx<=260)
					isSelect=true;
				//}
			}else if(actioncode==MotionEvent.ACTION_MOVE){
				nowx=event.getX();
				if(isDown){
					mode1.scrollBy((int)(currx-nowx), mode1.getScrollY());
					mode2.scrollBy((int)(currx-nowx), mode2.getScrollY());
					mode3.scrollBy((int)(currx-nowx), mode3.getScrollY());
					mode4.scrollBy((int)(currx-nowx), mode4.getScrollY());
					currx=nowx;
				}
			}else if(actioncode==MotionEvent.ACTION_UP){
				if(isDown){
					float lepas=event.getX();
					if(lepas-awal>100&&mode!=1){
						Log.d("W","Ukuran : "+mode1x+" , "+mode2x+" , "+mode3x);
						mode1x-=230;
						mode2x-=230;
						mode3x-=230;
						mode4x-=230;
						Log.d("W","Ukuran : "+mode1x+" , "+mode2x+" , "+mode3x);
						mode-=1;
					}else if(awal-lepas>100&&mode!=4){
						Log.d("W","Ukuran : "+mode1x+" , "+mode2x+" , "+mode3x);
						mode1x+=230;
						mode2x+=230;
						mode3x+=230;
						mode4x+=230;
						Log.d("W","Ukuran : "+mode1x+" , "+mode2x+" , "+mode3x);
						mode+=1;
					}else if(awal==lepas&&isSelect){
						nextAct2();						
					}
					mode1.scrollTo(mode1x, 0);
					mode2.scrollTo(mode2x, 0);
					mode3.scrollTo(mode3x, 0);
					mode4.scrollTo(mode4x, 0);
					Log.d("W","Ukuran : "+mode1.getScrollX()+" , "+mode2.getScrollX()+" , "+mode3.getScrollX());
					isDown=false;
					isSelect=false;
				}
			}
		}
		return false;
	}
	
	private void nextAct2() {
		GameSetting.isFirstTime=true;
		GameSettingHolder.modegame=mode;
		if(mode!=4) {
			this.startActivity(new Intent("android.intent.action.GAMESETTING"));
		}else{
			GameSound.changeMusicFromMainToGame(this);//ganti musik main menu jadi musik game
			GameSettingHolder.statusTutorial = true;//jika modenya adalah LATIHAN, otomatis hidupkan tutorial
			this.startActivity(new Intent("android.intent.action.GAMEPLAY"));
		}
		overridePendingTransition(R.anim.fade, R.anim.hold);
		this.finish();
	}
	
	
	private void helpact() {
		GameSettingHolder.modegame=mode;
		//this.startActivity(new Intent("android.intent.action.GAMEHELP"));
		//TODO : sementara buat testing multiplayer
		this.startActivity(new Intent("android.intent.action.MULTIPLAYER"));
		overridePendingTransition(R.anim.fade, R.anim.hold);		
		this.finish();
	}
}
